<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" :labelAlign="'left'">
        <a-row>
          <a-col :span="24">
            <div class="top-row">
              <a-form-item :colon="false" label="设备名称">
                {{ model.shebeiname }}
              </a-form-item>
              <a-form-item :colon="false" label="设备编号">
                {{ model.shebeibianhao }}
              </a-form-item>
              <a-form-item :colon="false" label="计划名称">
                {{ model.name }}
              </a-form-item>
              <a-form-item :colon="false" label="计划编号">
                {{ model.bianhao }}
              </a-form-item>
              <a-form-item :colon="false" label="点检周期（天）">
                {{ model.zhouqi }}
              </a-form-item>
              <a-form-item :colon="false" label="计划点检时间">
                {{ model.jihuadianjianshijian }}
              </a-form-item>
              <a-form-item :colon="false" label="负责人">
                {{ model.fuzeren_dictText }}
              </a-form-item>
            </div>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="基础信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jichuxinxi">
              <a-table :columns="xujianxiangmulie" :pagination="false" :dataSource="xujianxiangmu">
                <template slot="jieguo" slot-scope="text, record">
                  <div>
                    <a-input v-model="record.dianjianjieguo" width="100%" v-if="record.panduanbiaozhun === 1"></a-input>
                    <j-dict-select-tag type="list" v-model="record.dianjianjieguo" dictCode="xunjianjieguo"
                      v-if="record.panduanbiaozhun === 0" />
                  </div>
                </template>
                <template slot="beizhu" slot-scope="text, record">
                  <div>
                    <a-input v-model="record.xunjianbeizhu" width="100%"></a-input>
                  </div>
                </template>
              </a-table>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="巡检图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xunjiantupian">
              <j-image-upload isMultiple v-model="model.xunjiantupian"></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'GdDianjianRenwuForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: "/gongdan/gdDianjianRenwu/add",
        edit: "/gongdan/gdDianjianRenwu/edit",
        queryById: "/gongdan/gdDianjianRenwu/queryById",

        xunjianrenwuxiangmu: "/gongdan/gdDianjianRenwuXiangmu/list",
        xunjianrenwuxiangmuEdit: "/gongdan/gdDianjianRenwuXiangmu/editAll",
      },
      xujianxiangmu: [],
      xujianxiangmulie: [
        {
          title: '点检项目名称',
          align: "center",
          dataIndex: 'name'
        },
        {
          title: '检验方法',
          align: "center",
          dataIndex: 'fangfa'
        },
        {
          title: '检验标准',
          align: "center",
          dataIndex: 'jianyanbiaozhun'
        },
        {
          title: '判断标准',
          align: "center",
          dataIndex: 'panduanbiaozhun_dictText'
        },
        {
          title: '上限',
          align: "center",
          dataIndex: 'shangxian'
        },
        {
          title: '下限',
          align: "center",
          dataIndex: 'xiaxian'
        },
        {
          title: '点检结果',
          width: 150,
          align: "center",
          dataIndex: 'jieguo',
          scopedSlots: { customRender: 'jieguo' }
        },
        {
          title: '点检备注说明',
          width: 150,
          align: "center",
          dataIndex: 'xunjianbeizhu',
          scopedSlots: { customRender: 'beizhu' }
        },
      ],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    getXiangmus() {
      getAction(this.url.xunjianrenwuxiangmu, { renwu: this.model.id, pageSize: 10000000, }).then(res => {
        if (res.success) {
          this.xujianxiangmu = res.result.records
        }
      })
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
      this.getXiangmus()
    },
    submitForm() {
      const that = this;
      for(let i=0; i<this.xujianxiangmu.length; i++){
        if(this.xujianxiangmu[i].dianjianjieguo === undefined || this.xujianxiangmu[i].dianjianjieguo === null || this.xujianxiangmu[i].dianjianjieguo === ""){
          that.$message.warning("请填写完所有的巡检项目结果");
          return
        }
      }
      httpAction(this.url.xunjianrenwuxiangmuEdit, this.xujianxiangmu, "put").then((res) => {
        if (res.success) {
          httpAction(that.url.edit, that.model, "put").then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        } else {
          that.$message.warning(res.message);
        }
      }).finally(() => {
        that.confirmLoading = false;
      })
    },
  }
}
</script>
<style scoped>
.top-row {
  background: #F4F5F7;
  padding: 12px 24px;
  width: 100%;
  display: flex;
  justify-content: space-between;
}

.top-row /deep/ .ant-form-item {
  margin-bottom: 0;
  color: #6B778C
}

.top-row /deep/ .ant-form-item-label {
  line-height: 1;
}

.top-row /deep/ .ant-form-item-control {
  line-height: 1;
  margin-top: 10px;
}

.top-row /deep/ .ant-form-item-label>label {
  color: #6B778C
}

.basic-info-box,
.system-info-box {
  padding: 20px 0;
}

.basic-info-box /deep/ .ant-form-item,
.system-info-box /deep/ .ant-form-item {
  margin-bottom: 20px;
}

.basic-info-box /deep/ .ant-form-item-label>label {
  color: #172B4D;
}

.ene-w-h-d /deep/ .ant-collapse-borderless>.ant-collapse-item {
  border: none;
}

.ene-w-h-d /deep/ .ant-collapse-header {
  background: #F4F5F7;
}

.ene-w-h-d /deep/ .ant-collapse {
  background: transparent;
}

.ene-w-h-d /deep/ .ant-input[disabled] {
  background: #ebecf0
}

.ene-w-h-d /deep/ .ant-collapse-content>.ant-collapse-content-box {
  padding: 0
}
</style>